#include "SFMLInputManager.h"


SFMLInputManager::SFMLInputManager(void)
{
}


SFMLInputManager::~SFMLInputManager(void)
{
}

void SFMLInputManager::capture()
{
	m_keyLastFunctionStates = m_keyFunctionStates;
	for (auto it = m_keyFunctions.begin(); it != m_keyFunctions.end(); it++)
	{
		if (sf::Keyboard::isKeyPressed((*it).second))
		{
			m_keyFunctionStates.find((*it).first)->second = true;
		}
		else
		{
			m_keyFunctionStates.find((*it).first)->second = false;
		}
	}

	m_mousePosition.x = static_cast<float>(sf::Mouse::getPosition().x);
	m_mousePosition.y = static_cast<float>(sf::Mouse::getPosition().y);

	m_mouseLastFunctionStates = m_mouseFunctionStates;
	for (auto it = m_mouseFunctions.begin(); it != m_mouseFunctions.end(); it++)
	{
		if (sf::Mouse::isButtonPressed((*it).second))
		{
			m_mouseFunctionStates.find((*it).first)->second = true;
		}
		else
		{
			m_mouseFunctionStates.find((*it).first)->second = false;
		}
	}
}

const bool SFMLInputManager::isKeyDown(const std::string& p_id) const
{
	return m_keyFunctionStates.find(p_id)->second;
}

const bool SFMLInputManager::isKeyPressed(const std::string& p_id) const
{
	if (m_keyFunctionStates.find(p_id)->second && !m_keyLastFunctionStates.find(p_id)->second)
		return true;
	return false;
}

const bool SFMLInputManager::isKeyReleased(const std::string& p_id) const
{
	if (!m_keyFunctionStates.find(p_id)->second && m_keyLastFunctionStates.find(p_id)->second)
		return true;
	return false;
}

const bool SFMLInputManager::isMouseButtonDown(const std::string& p_id) const
{
	return m_mouseFunctionStates.find(p_id)->second;
}

const bool SFMLInputManager::isMouseButtonPressed(const std::string& p_id) const
{
	if (m_mouseFunctionStates.find(p_id)->second && !m_mouseLastFunctionStates.find(p_id)->second)
		return true;
	return false;
}

const bool SFMLInputManager::isMouseButtonReleased(const std::string& p_id) const
{
	if (!m_mouseFunctionStates.find(p_id)->second && m_mouseLastFunctionStates.find(p_id)->second)
		return true;
	return false;
}


void SFMLInputManager::addKeyFunction(const std::string& p_id, void* p_key)
{
	m_keyFunctions.insert(std::make_pair(p_id, (sf::Keyboard::Key)(int)p_key));
	m_keyFunctionStates.insert(std::make_pair(p_id, false));
}

void SFMLInputManager::addMouseFunction(const std::string& p_id, void* p_mouseBtn)
{
	m_mouseFunctions.insert(std::make_pair(p_id, *static_cast<sf::Mouse::Button*>(p_mouseBtn)));
	m_mouseFunctionStates.insert(std::make_pair(p_id, false));
}
